#include "EditorSideBarView.h"
#include "EditorSideBarViewModel.h"

#include "Widgets/RectangleWidget.h"
#include "Widgets/EditorImageButton.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/Positioners/HorizontalPositionerWidget.h"
#include "Widgets/ImageWidget.h"

#include "Ability/EditorAbilityView.h"
#include "Ability/EditorAbilityViewModel.h"

#include "Object/NewObject.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

void REditorSideBarView::BindViewModel(TSharedObjectPtr<REditorSideBarViewModel> InViewModel)
{
    ViewModel=InViewModel;

    for(int64_t i=0;i<ViewModel->GetAbilitysNum();++i)
    {
        auto AbilityViewModel=ViewModel->GetAbility(i);
        auto ViewModelClass=AbilityViewModel->GetClass();
        auto ViewClass=REditor::Get()->GetViewClass(ViewModelClass);
        auto AbilityView=NewObject<REditorAbilityView>(ViewClass);
        AbilityView->BindViewModel(AbilityViewModel);
        Abilitys.Add(AbilityView);
    }    
}

void REditorSideBarView::FillWidget(TSharedObjectPtr<RWidget> InParentWidget)
{
    auto Root=NewWidget<RWidget>(InParentWidget);
    Root->Anchor.Fill(InParentWidget);

    auto Background=NewWidget<RRectangleWidget>(Root);
    Background->Anchor.Fill(Root);
    Background->FillColor=REditor::Get()->CurrentTheme->BackgroundColor;

    //标题栏
    auto TitleBar=NewWidget<RWidget>(Root);
    TitleBar->Height=ViewModel->TitleHeight;
    TitleBar->Anchor.TopReference=Root;
    TitleBar->Anchor.HorizontalFill(Root);


    //标题栏左侧
    switch(ViewModel->GetActiveBarPosition())
    {
        case EEditorAbilityActiveBarPositionType::TopWithOperations:
        {

            auto TitleBarLeft=NewWidget<RHorizontalPositionerWidget>(TitleBar);
            TitleBarLeft->Anchor.Fill(TitleBar);
            
            for(int64_t Index=0; Index<ViewModel->GetAbilitysNum();Index++)
            {
                auto AbilityTab=NewWidget<RWidget>(TitleBar);
                AbilityTab->Width=ViewModel->TabSize;
                AbilityTab->Anchor.VerticalCenterReference=TitleBar;
                AbilityTab->Height=ViewModel->TabSize;

                auto IconImage=NewWidget<RImageWidget>(AbilityTab);
                IconImage->Anchor.CenterIn(AbilityTab);
                IconImage->Width=ViewModel->IconImageSize;
                IconImage->Height=ViewModel->IconImageSize;
                IconImage->Image=[this,Index](){
                    return this->ViewModel->GetAbilityIcon(Index);
                };

                auto MouseWidget=NewWidget<RMouseWidget>(AbilityTab);
                MouseWidget->Anchor.Fill(AbilityTab);
                MouseWidget->OnClicked.BindLambda([this,Index](const SMouseButtonEvent&){
                    this->ViewModel->OnAbilityClicked(Index);
                    return true;
                });
                MouseWidget->OnEntered.BindLambda([this,Index](){
                    this->ViewModel->HoveredAbilityIndex=Index;
                });
                MouseWidget->OnExited.BindLambda([this,Index](){
                    if(this->ViewModel->HoveredAbilityIndex==Index)
                    {
                        this->ViewModel->HoveredAbilityIndex=-1;
                    }
                });

                //a highlight bar at the bottom of the active ability
                auto Highlight=NewWidget<RRectangleWidget>(AbilityTab);
                Highlight->Anchor.BottomReference=AbilityTab;
                Highlight->Height=2.f;
                Highlight->Anchor.HorizontalFill(AbilityTab);
                Highlight->FillColor=
                    REditor::Get()->CurrentTheme->HighlightColor;
                Highlight->Visible=[this,Index](){
                    return this->ViewModel->IsAbilityHighlight(Index);
                };
            }

            break;
        }

        case EEditorAbilityActiveBarPositionType::None:
            //展示功能的名字
            for(int64_t AbilityIndex=0; AbilityIndex<ViewModel->GetAbilitysNum();AbilityIndex++)
            {
                auto TitleBarText=NewWidget<RTextWidget>(TitleBar);
                TitleBarText->Text=[this,AbilityIndex](){
                    return this->ViewModel->GetAbilityTitle(AbilityIndex);
                };
                TitleBarText->Visible=[this,AbilityIndex](){
                    return this->ViewModel->IsAbilityVisible(AbilityIndex);
                };
                TitleBarText->Anchor.VerticalFill(TitleBar);
                TitleBarText->VerticalAlignment=ETextVerticalAlignment::Center;
                TitleBarText->X=10;
                TitleBarText->Anchor.RightReference=TitleBar;

            }

            break;

        default:
            assert(false);
    }

    //操作按钮栏
    for(int64_t Index=0; Index<ViewModel->GetAbilitysNum();Index++)
    {
        //operation bar
        auto Overlay=NewWidget<RWidget>(TitleBar);
        Overlay->Anchor.Fill(TitleBar);
        Overlay->Visible=[this,Index](){
            return this->ViewModel->IsAbilityVisible(Index);
        };
        Abilitys[Index]->FillOperations(Overlay);
    }

    for(int64_t Index=0; Index<Abilitys.Num(); ++Index)
    {
        //create ability content
        auto AbilityContainer=NewWidget<RWidget>(Root);
        AbilityContainer->Anchor.TopReference=TitleBar;
        AbilityContainer->Anchor.TopReferenceEdge=EVerticalWidgetEdge::Bottom;
        AbilityContainer->Anchor.BottomReference=Root;
        AbilityContainer->Anchor.LeftReference=Root;
        AbilityContainer->Anchor.RightReference=Root;
        AbilityContainer->Visible=[this,Index](){
            return this->ViewModel->IsAbilityVisible(Index);
        };

        //AbilityContainer->bClipSubWidgets=true;
        Abilitys[Index]->CreateWidget(AbilityContainer);

    }

}
